package mkis.service.root.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mkis.service.root.data.model.Bbs;
import mkis.service.root.data.model.Member;
import mkis.service.root.service.BbsSevice;
import mkis.service.root.utils.MkisUtils;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;


@Controller
@RequestMapping(value="/bbs/notice_edit.do")
public class BbsEditCTR {
	
	@Resource(name="bbsService")
	private BbsSevice bbsService;
	
	
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView load(HttpServletRequest request,HttpServletResponse response,String boardSeq) throws Exception{
		ModelAndView mav=new ModelAndView("bbs/notice_edit");
		Map<String,Object> queryParam=new HashMap<String, Object>();
		
		Bbs bbs = null;
		String mode=null;
		if(boardSeq==null ||"".equals(boardSeq)){
			bbs = new Bbs();
			mode="insert";
		}else{
			bbs = bbsService.getNotice(boardSeq);
			mode="update";
		}
		
		mav.addObject("mode", mode);
		mav.addObject("bbs", bbs);
		return mav;
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public ModelAndView submit(HttpServletRequest request,HttpServletResponse response,Bbs bbs, String mode) throws Exception{
		ModelAndView mav=new ModelAndView("bbs/notice_edit_complete");
		
		bbs.setRegId(MkisUtils.getMember(request).getMb_id());
		if("insert".equals(mode)){
			bbsService.insertNotice(bbs);
		} else{
			bbsService.updateNotice(bbs);
		}
		
		mav.addObject("mode", mode);
		return mav;
	}
}
